package com.example.xd.tuiliu_test.videoplayer.lifecyclemanager;

import android.app.Activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.example.xd.tuiliu_test.videoplayer.exception.ActivityIsNullException;


public class LifecycleManager implements ILifecycleManager {
    static LifecycleManager manager;
    private String FRAGMENT_TAG = "com.ronglian.opengldemo.playerview.lifecyclemanager";

    public static LifecycleManager getInstance() {
        if (manager == null) {
            synchronized (LifecycleManager.class) {
                if (manager == null) {
                    manager = new LifecycleManager();
                }
            }
        }
        return manager;
    }

    private LifecycleManager() {

    }

    @Override
    public void registerPlayer(ILifecycle lifecycle) {
        Activity activity = lifecycle.getActivityForLifecycle();
        Fragment fragment = lifecycle.getFragmentForLifecycle();
        if (activity == null && fragment == null) {
            throw new ActivityIsNullException();
        }
        if (activity != null) {
            LifecycleManagerFragment lifecycleManagerFragment = bindLifecycle(activity);
            lifecycleManagerFragment.registerILifecycle(lifecycle);
        }
        if (fragment != null) {
            LifecycleManagerFragment lifecycleManagerFragment = bindLifecycle(fragment);
            lifecycleManagerFragment.registerILifecycle(lifecycle);
        }
    }

    @Override
    public void unRegisterPlayer(ILifecycle lifecycle) {
        Activity activity = lifecycle.getActivityForLifecycle();
        Fragment fragment = lifecycle.getFragmentForLifecycle();
        if (activity == null && fragment == null) {
            throw new ActivityIsNullException();
        }
        if (activity != null) {
            FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
            LifecycleManagerFragment fragmentByTag = (LifecycleManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
            if (fragmentByTag != null) {
                fragmentByTag.removeILifecycle(lifecycle);
            }
        }
        if (fragment != null) {
            FragmentManager childFragmentManager = fragment.getChildFragmentManager();
            LifecycleManagerFragment fragmentByTag = (LifecycleManagerFragment) childFragmentManager.findFragmentByTag(FRAGMENT_TAG);
            if (fragmentByTag != null) {
                fragmentByTag.removeILifecycle(lifecycle);
            }
        }
    }

    LifecycleManagerFragment bindLifecycle(Fragment fragment) {
        FragmentManager childFragmentManager = fragment.getChildFragmentManager();
        Fragment fragmentByTag = childFragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (fragment != null) {
            return (LifecycleManagerFragment) fragmentByTag;
        } else {
            LifecycleManagerFragment lifecycleManagerFragment = new LifecycleManagerFragment();
            childFragmentManager.beginTransaction().add(lifecycleManagerFragment, FRAGMENT_TAG).commitAllowingStateLoss();
            return lifecycleManagerFragment;
        }
    }

    LifecycleManagerFragment bindLifecycle(Activity activity) {
        FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
        Fragment fragmentByTag = fm.findFragmentByTag(FRAGMENT_TAG);
        if (fragmentByTag != null) {
            return (LifecycleManagerFragment) fragmentByTag;
        } else {
            LifecycleManagerFragment lifecycleManagerFragment = new LifecycleManagerFragment();
            fm.beginTransaction().add(lifecycleManagerFragment, FRAGMENT_TAG).commitAllowingStateLoss();
            return lifecycleManagerFragment;
        }
    }
}
